package ATM;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
 * 转账
 */
public class TransferPanel extends JPanel {
    private JLabel user = new JLabel("输入转账用户名：");
    private JLabel blance = new JLabel("输入转账金额：");
    private JTextField userText = new JTextField();
    private JTextField blanceText = new JTextField();
    private JButton sureBtn = new JButton(new ImageIcon("src/ATM/image/buttonSure.jpg"));
    private JButton endBtn = new JButton(new ImageIcon("src/ATM/image/buttonOff.jpg"));
    private Font font = new Font("宋体", Font.BOLD,25);
    public TransferPanel(ATMFrame atm){
        this.setLayout(null);
        this.setOpaque(false);

        user.setBounds(300,200,220,50);
        user.setFont(font);
        blance.setBounds(300,300,220,50);
        blance.setFont(font);
        userText.setBounds(530,200,220,50);
        userText.setFont(font);
        blanceText.setBounds(530,300,220,50);
        blanceText.setFont(font);
        sureBtn.setBounds(60,600,140,50);
        sureBtn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String otherAccount = userText.getText();
                // 不能转账给自己
                if(otherAccount.equals(atm.getUsers().get(atm.getIndex()).getAccount())){
                    JOptionPane.showMessageDialog(TransferPanel.this,"不能转给自己");
                    return;
                }
                // 判断对方账号是否存在
                boolean isExist = false;
                int otherIndex = 0;
                for(int i = 0;i < atm.getUsers().size();i++){
                    if(atm.getUsers().get(i).getAccount().equals(otherAccount)){
                        isExist = true;
                        otherIndex = i;
                        break;
                    }
                }
                if(!isExist){
                    JOptionPane.showMessageDialog(TransferPanel.this,"输入的账号不存在");
                    return;
                }
                try {
                    double blance = Double.parseDouble(blanceText.getText());
                    // 判断余额是否足够
                    if(blance > atm.getUsers().get(atm.getIndex()).getBlance()){
                        JOptionPane.showMessageDialog(TransferPanel.this,"您的余额不足");
                        return;
                    }else if (blance <= 0){
                        JOptionPane.showMessageDialog(TransferPanel.this,"金额必须大于0");
                    }else {
                        // 从当前账号上扣去转账的金额
                        atm.getUsers().get(atm.getIndex()).setBlance(atm.getUsers().get(atm.getIndex()).getBlance() - blance);
                        // 将转账的金额加在对方账号上
                        atm.getUsers().get(otherIndex).setBlance(atm.getUsers().get(otherIndex).getBlance() + blance);
                        FileUtil.writeObject(atm.getUsers(), atm.getFILE_PATH());
                        JOptionPane.showMessageDialog(TransferPanel.this,"转账成功");
                        atm.showPanel("menu");
                    }

                }catch (Exception k){
                    JOptionPane.showMessageDialog(TransferPanel.this,"金额输入错误，转账失败");
                }
            }
        });
        endBtn.setBounds(824,600,140,50);
        endBtn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                atm.showPanel("menu");
            }
        });

        this.add(user);
        this.add(blance);
        this.add(userText);
        this.add(blanceText);
        this.add(sureBtn);
        this.add(endBtn);
    }

    public void Refresh(){
        userText.setText("");
        blanceText.setText("");
    }

}
